//   CKSAppStorageFarmConfiguration.EventReceiver.cs
//   Copyright (c) 2012 Eswar Prakash
// 
//    This library is free software; you can redistribute it and/or modify  
//    it  under the terms of the GNU Lesser General Public License version  
//    2.1 as published by the Free Software Foundation.                     
//                                                                          
//    This library is distributed in the hope that it will be useful, but   
//    WITHOUT ANY WARRANTY; without even the implied warranty of            
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     
//    Lesser General Public License for more details.                       
//                                                                          
//    You should have received a copy of the GNU Lesser General Public      
//    License along with this library; if not, write to the Free Software   
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  
//    USA
namespace CKSAS.AppStorage
{
    using System;
    using System.Runtime.InteropServices;
    using System.Security.Permissions;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Security;
    using Microsoft.SharePoint.Administration;
    using SharedService;

    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid("9a0965c0-0c62-45dc-b96f-5de690a31832")]
    internal class CKSAppStorageFarmConfigurationEventReceiver : SPFeatureReceiver
    {
        /// <summary>
        /// Occurs after a Feature is activated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"/> object that represents the properties of the event.</param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            var localFarm = SPFarm.Local;
            var localServer = SPServer.Local;

            // check if the service is already installed
            var service = localFarm.Services.GetValue<AppStorageService>();
            if (service == null)
            {
                service = new AppStorageService(localFarm);
                service.Status = SPObjectStatus.Online;
                service.Update();
            }

            // now ensure a service instance
            if (service.Instances.Count == 0)
            {
                var serviceInstance = new AppStorageServiceInstance(localServer, service);
                serviceInstance.Update(true);
            }

            // now create the proxy for the service
            var serviceProxy = localFarm.ServiceProxies.GetValue<AppStorageServiceProxy>();
            if (serviceProxy == null)
            {
                serviceProxy = new AppStorageServiceProxy(localFarm);
                serviceProxy.Update(true);
            }
        }

        /// <summary>
        /// Occurs when a Feature is deactivated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"/> object that represents the properties of the event.</param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            var localFarm = SPFarm.Local;
            var localServer = SPServer.Local;

            // remove any service instances first
            var instance = localServer.ServiceInstances.GetValue<AppStorageServiceInstance>();
            while (instance != null)
            {
                localServer.ServiceInstances.Remove(instance.Id);
                instance = localServer.ServiceInstances.GetValue<AppStorageServiceInstance>();
            }

            // remove service proxies
            var serviceProxy = localFarm.ServiceProxies.GetValue<AppStorageServiceProxy>();
            if (serviceProxy != null)
            {
                // remove any application proxies
                foreach (var serviceAppProxy in serviceProxy.ApplicationProxies)
                {
                    serviceAppProxy.Unprovision(true);
                    serviceAppProxy.Delete();
                }

                localFarm.ServiceProxies.Remove(serviceProxy.Id);
            }

            // remove any services
            var service = localFarm.Services.GetValue<AppStorageService>();

            if (service != null)
            {
                // remove any applications
                foreach (var serviceApplication in service.Applications)
                {
                    serviceApplication.Unprovision(true);
                    serviceApplication.Delete();
                }

                localFarm.Services.Remove(service.Id);
            }
        }
    }
}